<?php

/**
 * uniBoard API User Rank
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-08 03:38:35 +0000 (Wed, 08 Sep 2010) $
 * $Revision: 60 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class api_user_rank extends class_api{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore				= NULL;
	
	/**
	 * Rank ID
	 *
	 * @var integer
	 */
	
	public $rank_id 				= 0;
	
	/**
	 * Rank Special
	 *
	 * @var bool
	 */
	
	public $rank_special			= false;
	
	/**
	 * Rank Posts
	 *
	 * @var integer
	 */
	
	public $rank_posts				= 0;
	
	/**
	 * Rank Name
	 *
	 * @var string
	 */
	
	public $rank_name 				= NULL;
	
	/**
	 * Rank Image
	 *
	 * @var string
	 */
	
	public $rank_image 				= NULL;
	
	/**
	 * Rank Stars
	 *
	 * @var integer
	 */
	
	public $rank_stars				= 1;
					
	// ------------------------------------------------------------------------

	/**
	 * Initialise Api
	 *
	 * @param object $appCore
	 * @param integer $target_id
	 */
	
	function __construct( $appCore, $target_id = 0){
					
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Store Mask ID
		$this -> rank_id	= (integer) $target_id;
		
		// Autoload If Rank ID is bigger than zero
		if ( $target_id > 0)
		{
			// Make select
			$appCore -> db -> query( 'SELECT *
			FROM ' . DB_PREFIX . 'user_ranks
			WHERE rank_id = \'' . $this -> rank_id . '\'
			LIMIT 1');
			
			// Mask found
			if ( $rank_result = $appCore -> db -> fetch_assoc())
			{
				$this -> data_loaded		= true;
				
				$this -> rank_special		= $rank_result['rank_special'];
				$this -> rank_posts		 	= $rank_result['rank_posts'];
				$this -> rank_name		 	= $rank_result['rank_name'];
				$this -> rank_image		 	= $rank_result['rank_image'];
				$this -> rank_stars		 	= $rank_result['rank_stars'];
			}
			else
			{
				//give action an error
				$this -> setError( $appCore -> lang['acp_rank_not_found']);
			}
			
			// Free result
			$appCore -> db -> freeResult();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Save API data to DB
	 *
	 */
	
	function saveData(){
		
		// Do mysql query only when we have changes, and no errors
		if ( !empty( $this -> changes) && !$this -> error)
		{	
			// Insert?
			if ( $this -> rank_id == 0)
			{
				// Insert new record
				$this -> appCore -> db -> insert( $this -> changes, 'user_ranks');
			
				// Store new mask id
				$this -> rank_id = $this -> appCore -> db -> insertId();
			}
			else
			{
				// Update record
				$this -> appCore -> db -> update( $this -> changes, 'user_ranks', 'rank_id = \'' . $this -> rank_id . '\'');
			}
		
			// Flush cache
			class_cache::flushCache( 'user_ranks');
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Delete API data to DB
	 *
	 */
	
	function deleteData(){
		
		// Delete data
		$this -> appCore -> db -> delete( 'user_ranks', 'rank_id = \'' . $this -> rank_id . '\'');
		
		// Flush cache
		class_cache::flushCache( 'user_ranks');
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set rank_special
	 *
	 * @param bool $new_value
	 */
	
	function setRankSpecial( $new_value){
		
		// Store in object
		$this -> rank_special = (bool) $new_value;
		
		// Add to made changes
		$this -> changes['rank_special'] = $this -> rank_special;
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set rank_posts
	 *
	 * @param integer $new_value
	 */
	
	function setRankPosts( $new_value){
		
		// Store in object
		$this -> rank_posts = (integer) $new_value;
		
		// Fix Minimal Value?
		if ( $this -> rank_posts < 0)
		{
			$this -> rank_posts = 0;
		}
		
		// Add to made changes
		$this -> changes['rank_posts'] = $this -> rank_posts;
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set rank_name
	 *
	 * @param string $new_value
	 */
	
	function setRankName( $new_value){
		
		if ( isset( $new_value[0]))
		{
			// Store in object
			$this -> rank_name = $new_value;
			
			// Check max lenght
			if ( mb_strlen( $new_value) > 255)
			{
				// Set error
				$this -> setError( $this -> appCore -> lang['acp_rank_name_too_long']);
			}
			else
			{
				// Add to made changes
				$this -> changes['rank_name'] = $this -> appCore -> db -> strEscape( $this -> rank_name);
			}
		}
		else
		{
			// Set error
			$this -> setError( $this -> appCore -> lang['acp_rank_name_empty']);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set rank_image
	 *
	 * @param string $new_value
	 */
	
	function setRankImage( $new_value){
	
		// Store in object
		$this -> rank_image = $new_value;
		
		// Remove DIR escapes
		$this -> rank_image = str_replace( '../', '', $this -> rank_image);
		$this -> rank_image = str_replace( '..\\', '', $this -> rank_image);
		
		// Add to made changes
		$this -> changes['rank_image'] = $this -> appCore -> db -> strEscape( $this -> rank_image);
	
	}
		
	// ------------------------------------------------------------------------

	/**
	 * Set rank_stars
	 *
	 * @param integer $new_value
	 */
	
	function setRankStars( $new_value){
		
		// Store in object
		$this -> rank_stars = (integer) $new_value;
		
		// Fix Minimal Value?
		if ( $this -> rank_stars < 1)
		{
			$this -> rank_stars = 1;
		}
		
		// Just in case, API wont allow more than 32 pips
		if ( $this -> rank_stars > 32)
		{
			$this -> rank_stars = 32;
		}
		
		// Add to made changes
		$this -> changes['rank_stars'] = $this -> rank_stars;
		
	}
	
}